<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>修改密码</title>
    <style>
        body {
            font-family: "Helvetica Neue", sans-serif;
            background-color: #f9f9f9;
            margin: 30px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        form {
            max-width: 400px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }

        label {
            display: block;
            margin: 12px 0 6px;
            font-weight: 600;
        }

        input[type="password"] {
            width: 100%;
            padding: 8px 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        button {
            margin-top: 20px;
            width: 100%;
            padding: 10px;
            background-color: #2c3e50;
            border: none;
            color: white;
            font-size: 16px;
            border-radius: 4px;
            cursor: pointer;
        }

        button:hover {
            background-color: #1a2733;
        }

        .info {
            text-align: center;
            color: red;
            margin-top: 10px;
        }
    </style>
    <script>
        function updatePassword() {
            const oldPwd = document.getElementById("old_pwd").value.trim();
            const newPwd = document.getElementById("new_pwd").value.trim();
            const rePwd = document.getElementById("re_pwd").value.trim();

            if (!oldPwd || !newPwd || !rePwd) {
                alert("请填写完整信息！");
                return false;
            }

            if (newPwd !== rePwd) {
                alert("两次新密码不一致！");
                return false;
            }

            fetch("/user/updatePwd", {
                method: "PATCH",
                headers: {
                    "Content-Type": "application/json",
                    "Authorization": localStorage.getItem("token") || ""
                },
                body: JSON.stringify({
                    old_pwd: oldPwd,
                    new_pwd: newPwd,
                    re_pwd: rePwd
                })
            }).then(res => res.json())
                .then(data => {
                    if (data.code === 0) {
                        alert("密码修改成功，请重新登录！");
                        window.location.href = "/login";
                    } else {
                        alert("修改失败：" + data.message);
                    }
                });

            return false; // 阻止表单默认提交
        }
    </script>
</head>
<body>
<h1>修改密码</h1>
<form onsubmit="return updatePassword()">
    <label for="old_pwd">原密码</label>
    <input type="password" id="old_pwd" name="old_pwd" required>

    <label for="new_pwd">新密码</label>
    <input type="password" id="new_pwd" name="new_pwd" required>

    <label for="re_pwd">确认新密码</label>
    <input type="password" id="re_pwd" name="re_pwd" required>

    <button type="submit">提交修改</button>
</form>
</body>
</html>
